import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import Controls from '#cpt/temp/Controls'
import Confirm from '#cpt/temp/Confirm'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 	 = $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 	= $Lazy.file(()=>import('#private/cache-api'))
const ControlModalAlter		= $Lazy.state(()=>import('./cpt/control-modal-alter'))
const statusData = [{ name:'待审核', value:'0' },{ name:'审核通过', value:'2' },{ name:'审核未通过', value:'3' }]
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		selectedRows:[],
		// 搜索表单列表
		// 所有的搜索医院，项目用下拉带搜索 20211012 耿琰
		// 项目名称修改为文本框搜索，列表新增申请时间 20211130 耿琰
		forms : [
			{ label: '医院名称',		name: 'hosp_id', type: 'select', nameStr:'name'},
			{ label: '物价代码',		name: 'price_code' },
			{ label: '项目名称',		name: 'item_name' },
			// { label: '项目名称',		name: 'kind_id', type: 'select' },
			{ label: '申请人',		name: 'operator' },
			{ label: '审核人',		name: 'checkuser' },
			{ label: '审核状态',  	name: 'status', type:'select', data:statusData },
		],
	}
	
	model = {}
	query = {type:'sub'}
	
	componentDidMount(){
		this.getSelect()
		if(window.isInit){
			this.fetch()
		}
	}
	
	// 获取下拉数据
	getSelect = () => {
		const { forms } = this.state
		$cache.then( async f => {
			// 医院
			$fn.setRow(forms, 'hosp_id', await f.getCache({ api:'hospital' } ) )
			// 项目名称
			// $fn.setRow( forms, 'kind_id', await f.getCache({ api:'labkindselect' } ) )
			this.setState({ forms })
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'qt-hospital-price-audit/index',{isIndex:true})
	
	// 表单
	cols = [
		{ title: '医院名称',			dataIndex: 'hosp_name', 		width:150 },
		{ title: '物价代码',			dataIndex: 'price_code', 		width:100 },
		{ title: '关联物价', 		dataIndex: 'price_rel_codes', 	width:150 },
		{ title: '项目名称', 		dataIndex: 'item_name', 		width:100 },
		{ title: '申请人', 			dataIndex: 'operator', 			...$fn.table.name },
		{ title: '审核人', 			dataIndex: 'checkuser', 		...$fn.table.name },
		{ title: '标准价格(元)', 		dataIndex: 'source_price', 		...$fn.table.num },
		{ title: '结算价格(元)', 		dataIndex: 'price', 			...$fn.table.num },
		{ title: '备注', 			dataIndex: 'remark', 			width:150 },
		{ title: '申请时间', 		dataIndex: 'created_at', 		...$fn.table.time },
		{ title: '审核时间', 		dataIndex: 'update_at', 		...$fn.table.time },
		{ title: '状态', 			dataIndex: 'status', 			...$fn.table.status, render:text =>$fn.getState(statusData,text) },
	]
	
	// 操作按钮
	controls = () => {
		const { selectedKeys, selectedRows } = this.state
		let uuids = []
		selectedRows.map(v=>{
			if(+v.status === 0) uuids.push(v.uuid)
			return uuids
		})
		return [
			// 撤回
			Confirm.call(this, '撤回', 'qt-hospital-price-audit/del',{ msg:`确认撤回【${uuids.length}】条未审核数据的物价吗？`, isKeys:true, query:{uuids}} ),
			// 确认
			Confirm.call(this, '确认', 'qt-hospital-price-audit/complete',{ msg:`共计【${selectedKeys.length}】条数据，是否确认审核通过？`, isKeys:true, query:(keys,rows)=> {
				let uuid = []
				rows.forEach((v, i)=> {
					uuid[i] = v.uuid
				})
				return {uuid}
			}} ),
			// 物价异常
			Controls.click.call(this, '物价异常', ()=>this.modalalter.open(selectedRows), {isKeys:true} ),
			// 导出
			Controls.exports.call(this, 'qt-hospital-price-audit/index')
		]
	}
	
	render(){
		return (
			<>
				<Page title='物价审核' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this,{isIndex:true, isInit: window.isInit}) }
				</Page>
				{/* 物价异常 */}
				<ControlModalAlter onRef={ref=>this.modalalter=ref} onRefresh={this.fetch} />
			</>
		)
	}
}

export default Index